package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.QualityEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TopEntity;
import com.qianfeng.wuyang.himalaya.callback.RecommendFocusImgService;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by WangQing on 2016/12/10.
 */

public class QualityRecommendAdapter extends RecyclerView.Adapter<QualityRecommendAdapter.ViewHolder> implements Callback<TopEntity>, View.OnClickListener {
    private Context context;
    private List<QualityEntity> list = new ArrayList<>();

    public QualityRecommendAdapter(Context context) {
        this.context = context;

    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        //推荐焦点图service
        RecommendFocusImgService recommendFocusImgService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecommendFocusImgService.class);
        recommendFocusImgService
                .getFocusimgList("and-a1","android","true","true",2,"4.3.98")
                .enqueue(this);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recommend_quality_item,parent,false);
        ViewHolder holder = new ViewHolder(view);


        return holder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (list.size()>0) {

            ((TextView)holder.titleLayoutabove.getChildAt(0)).setText(list.get(position).getTitle());
            ((TextView)holder.titleLayoutabove.getChildAt(1)).setText(list.get(position).getSubtitle());
            ((TextView)holder.titleLayoutabove.getChildAt(2)).setText(list.get(position).getFootnote());
            if (list.get(position).getCoverPath()!=null) {
                Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.img);
            }
            holder.layout.setOnClickListener(this);

        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onResponse(Call<TopEntity> call, Response<TopEntity> response) {
        if (response.body().getSpecialColumn()!=null) {
            list.addAll(response.body().getSpecialColumn().getList());

        }
    }

    @Override
    public void onFailure(Call<TopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(context,t.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);
        Toast.makeText(context,"精品听单"+position,Toast.LENGTH_SHORT).show();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private final ImageView img;
        private final LinearLayout layout;
        private  LinearLayout titleLayoutabove,titleLayoutbelow;
        private final ImageView imgbelow;

        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.recommendquality_image_1);
            titleLayoutabove = (LinearLayout) itemView.findViewById(R.id.recommendquality_titlelayout1);
            imgbelow = (ImageView) itemView.findViewById(R.id.recommendquality_image_2);
            titleLayoutbelow = (LinearLayout) itemView.findViewById(R.id.recommendquality_titlelayout2);

            layout = (LinearLayout) itemView.findViewById(R.id.recommend_quality_itemlayout);
        }
    }
}
